<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Topic Model
 *
 * @package		SimplyPost
 * @subpackage	Topic
 * @category	Model
 * @author		Pascal Kriete
 */

class Top_model extends Model {
	
	/**
	 * Constructor
	 *
	 * @access	public
	 */
    function Top_model()
    {
        parent::Model();
    }

	// --------------------------------------------------------------------

	/**
	 * Get a single topic
	 *
	 * @access	private
	 * @param	integer	topic id
	 * @return	mixed	topic information
	 */
	function get($id)
	{
		$query = $this->db->get_where('topics', array('id' => $id), 1, 0);
		return $query->row_array();
	}
	
	// --------------------------------------------------------------------

	/**
	 * Get parent information
	 *
	 * @access	private
	 * @param	integer	parent id
	 * @return	mixed	parent information
	 */
	function get_info($id)
	{
		$this->db->select('id, name, parent_id');
		$this->db->from('forums');
		$this->db->where('id', $id);
		$query = $this->db->get('forums');
		
        if ($query->num_rows < 1)
            return FALSE;
		else
			return $query->result_array();
	}
	
	// --------------------------------------------------------------------
    
	/**
	 * Get all topics
	 *
	 * @access	private
	 * @param	integer	parent id
	 * @return	mixed	topic information
	 */
    function get_topics($id)
    {
		$this->db->where('parent_id', $id);
		$this->db->order_by('newest_time', 'desc');
		$query = $this->db->get('topics');
		
        if ($query->num_rows < 1)
            return array(-1);
		else
			return $query->result_array();
    }

	// --------------------------------------------------------------------

	/**
	 * Create new topic
	 *
	 * @access	private
	 * @param	string	topic name
	 * @param	integer	parent forum id
	 * @return	integer	new topic id
	 */
	function create($content, $id)
	{
		$this->db->insert('topics', array('name' => $content, 'parent_id' => $id));
		return $this->db->insert_id();
	}
	
	// --------------------------------------------------------------------

	/**
	 * Update a topic
	 *
	 * @access	private
	 * @param	integer	topic id
	 * @param	mixed	updated topic data
	 * @return	void
	 */
	function update($id, $data)
	{
		$this->db->where('id', $id);
		$this->db->update('topics', $data);
	}
	
	// --------------------------------------------------------------------

	/**
	 * Delete a topic
	 *
	 * @access	private
	 * @param	integer	topic id
	 * @return	void
	 */
	function delete($id) {
		$this->db->delete('posts', array('parent_id' => $id));
		$this->db->delete('topics', array('id' => $id));
	}
}